package com.avicit.zipkinkafkaesserver.dao;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.util.StringUtils;
import zipkin2.Span;
import zipkin2.elasticsearch.ElasticsearchStorage;
import zipkin2.storage.QueryRequest;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 操作es
 */
@Repository
public class SpanDao {

    @Autowired
    private ElasticsearchStorage elasticsearchStorage;

    public List<List<Span>> spanListFromES() throws IOException {
        return elasticsearchStorage.spanStore().getTraces(QueryRequest
                .newBuilder()
                .lookback(1080000000)//倒叙时间 ms
                .limit(10)
//                .maxDuration(5000000L)
//                .minDuration(1L)//us
                .endTs(System.currentTimeMillis())//截至时间
                .build()).execute();
    }

    /**
     * 按时间段查询
     * @param startTime  开始时间 ms
     * @param endTime 结束时间 ms
     * @param limit 取多少条
     * @return
     * @throws IOException
     */
    public List<List<Span>> spanListFromES(long startTime, long endTime, int limit) throws IOException {
        QueryRequest build = QueryRequest
                .newBuilder()
                .lookback(endTime - startTime)//倒叙时间 ms
                .limit(limit)//默认取10
                .endTs(endTime)//截至时间
                .build();
        return elasticsearchStorage.spanStore().getTraces(build).execute();
    }
    /**
     * 按时间段查询
     * @param startTime  开始时间 ms
     * @param endTime 结束时间 ms
     * @param limit 取多少条
     * @return
     * @throws IOException
     */
    public List<List<Span>> spanListFromES(long startTime, long endTime, int limit,String interfaceName) throws IOException {

        Map<String,String> tags = new HashMap<String, String>();
        tags.put("http.path",interfaceName);
        return elasticsearchStorage.spanStore().getTraces(QueryRequest
                .newBuilder()
                .annotationQuery(tags)
                .lookback(endTime-startTime)//倒叙时间 ms
                .limit(limit)//默认取10
                .endTs(endTime)//截至时间
                .build()).execute();
    }

    /**
     * 按时间段查询
     * @param startTime  开始时间 ms
     * @param endTime 结束时间 ms
     * @param limit 取多少条
     * @return
     * @throws IOException
     */
    public List<List<Span>> spanListFromES(long startTime, long endTime, int limit,String serverName,String interfaceName) throws IOException {

        Map<String,String> tags = new HashMap<String, String>();
        tags.put("http.path",interfaceName);
        //默认
        QueryRequest build = QueryRequest
                .newBuilder()
                .lookback(endTime - startTime)//倒叙时间 ms
                .limit(limit)//默认取10
                .endTs(endTime)//截至时间
                .build();
        if (!StringUtils.isEmpty(serverName) && StringUtils.isEmpty(interfaceName)) {
             build = QueryRequest
                    .newBuilder()
                    .lookback(endTime - startTime)//倒叙时间 ms
                    .limit(limit)//默认取10
                    .endTs(endTime)//截至时间
                    .serviceName(serverName).build();
        }
        if (StringUtils.isEmpty(serverName) && !StringUtils.isEmpty(interfaceName)) {
            build = QueryRequest
                    .newBuilder()
                    .lookback(endTime - startTime)//倒叙时间 ms
                    .limit(limit)//默认取10
                    .endTs(endTime)//截至时间
                    .annotationQuery(tags)
                    .build();
        }
        if (!StringUtils.isEmpty(serverName) && !StringUtils.isEmpty(interfaceName)) {
             build = QueryRequest
                    .newBuilder()
                    .lookback(endTime - startTime)//倒叙时间 ms
                    .limit(limit)//默认取10
                    .endTs(endTime)//截至时间
                    .serviceName(serverName)
                     .annotationQuery(tags)
                     .build();
        }

        return elasticsearchStorage.spanStore().getTraces(build).execute();
    }




}
